<?php
/*
* simple class library for securing links
*/
class Url_auth
{
	private $ci;
	
	public function __construct()
	{
		$this->ci =& get_instance();
		$this->_set_url_auth();
	}
	
	public function get_auth()
	{
		return $this->ci->session->userdata('auth_link');
	}
	
	public function des_auth()
	{
		$this->ci->session->set_userdata('auth_link','');
	}
	
	public function val_auth($link)
	{
		$link = trim(htmlspecialchars($link));
		return $this->get_auth() == $link;
	}
	
	private function _set_url_auth()
	{
		if($this->ci->session->userdata('auth_link') == FALSE)
		{
			$this->ci->session->set_userdata('auth_link',$this->generate_key());
		}
	}
	
	//generate random key
	private function generate_key()
	{
		$salt = sha1(rand(1000,9999)).sha1(uniqid(md5(rand(100,999).sha1(rand(10000,99999)))));
		return substr(base64_encode(sha1(time())),0,5).substr(base64_encode(base64_encode(base64_encode($salt))),0,40).substr(base64_encode(md5(time())),0,5);
	}
}